/*
Script: Array.js

Copyright 2005-2007 Netvibes Ltd.
All rights reserved.

Credits:
    Partially based on MooTools Framework - copyright (c) 2007 Valerio Proietti, <http://mad4milk.net>
*/

// Standard Javascript 1.6 methods

UWA.merge(Array.prototype, {

	forEach: function(fn, bind){
		for (var i = 0, j = this.length; i < j; i++) fn.call(bind, this[i], i, this);
	},

	filter: function(fn, bind){
		var results = [];
		for (var i = 0, j = this.length; i < j; i++){
			if (fn.call(bind, this[i], i, this)) results.push(this[i]);
		}
		return results;
	},

	map: function(fn, bind){
		var results = [];
		for (var i = 0, j = this.length; i < j; i++) results[i] = fn.call(bind, this[i], i, this);
		return results;
	},

	every: function(fn, bind){
		for (var i = 0, j = this.length; i < j; i++){
			if (!fn.call(bind, this[i], i, this)) return false;
		}
		return true;
	},

	some: function(fn, bind){
		for (var i = 0, j = this.length; i < j; i++){
			if (fn.call(bind, this[i], i, this)) return true;
		}
		return false;
	},

	indexOf: function(item, from){
		var len = this.length;
		for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
			if (this[i] === item) return i;
		}
		return -1;
	}

});

// Deprecated and obsolete - backward compatibility with competitor UWA implementation

Array.prototype.normalize = function(sum) {
    var x = 0;
    var ratio = sum / this.inject(0, function(a, n) { return a + n; } );
    for (var i = 0; i < this.length - 1; i++) x += (this[i] *= ratio);
    this[this.length - 1] = sum - x;
}

Array.prototype.equals = function(compare) {
  if (!compare) {
    return false;
  }
  var len = this.length;
  if (len != compare.length) {
    return false;
  }
  for (var i = 0; i < len; i++) {
    if (this[i] != compare[i]) {
      return false;
    }
  }
  return true;
}